<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

if( isset( $_POST[ 'army_id' ] ) ) {
	$statement = $dal->prepare( 'SELECT officer_id 
		FROM army
		WHERE army_id = :army_id AND ruler_id = :ruler_id' );
	$statement->bindParam( ':army_id', $_POST[ 'army_id' ], PDO::PARAM_INT );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$army = $dal->getObject( $statement );
	
	if( isset( $army ) ) {
		$status = '';
		
		$statement = $dal->prepare( 'SELECT COUNT(*) 
			FROM army_orders 
			WHERE army_id = :army_id' );
		$statement->bindParam( ':army_id', $_POST[ 'army_id' ], PDO::PARAM_INT );
		$totalOrders = $dal->getValue( $statement );
		
		if( $totalOrders < 6 ) {
			if( isset( $_POST[ 'gather_at_location_id' ] ) && $_POST[ 'gather_at_location_id' ] != -1 ) {
				// First we need to make sure this is a valid location
				$location = validateLocation( $_POST[ 'gather_at_location_id' ] );
				if( isset( $location ) ) {
					// Get maximum time to get to the location
					$maxTime = getHighestTravelTimeSubordinates( $army->officer_id, $location->x, $location->y );
					
					// Set movement orders for all armies and armies following them
					// And after a wait order to fill the maximum time
					setTravelSubordinates( $army->officer_id, -1, $location->x, $location->y, $maxTime );
					$statsu = translate( 'All subordinate armies instructed to move, including allied armies following ours' );
				}
			}
			// Add more orders here
		}
		else {
			$status = translate( 'Order queue is already full' );
		}
		
		if( $status != '' ) {
			$_SESSION[ 'que_order_status' ] = $status;
		}
		header( 'Location: army.php?name=' . $army->name );
	}
	else {
		header( 'Location: armies.php' );
	}
}
else {
	header( 'Location: armies.php' );
}

function validateLocation( $locationId ) {
	global $dal;
	$validLocation = false;
	$statement = $dal->prepare( 'SELECT x, y, ruler_id 
		FROM locations 
		WHERE location_id = :location_id' );
	$statement->bindParam( ':location_id', $locationId, PDO::PARAM_INT );
	$location = $dal->getObject( $statement );
	if( isset( $location ) ) {
		if( $location->ruler_id == $_SESSION[ 'ruler_id' ] ) {
			$validLocation = true;
		}
		else {
			$statement = $dal->prepare( 'SELECT faction_id 
				FROM faction_member 
				WHERE ruler_id = :ruler_id AND invited = 0 AND requested = 0' );
			$rulerId = $_SESSION[ 'ruler_id' ];
			$statement->bindParam( ':ruler_id', $rulerId, PDO::PARAM_INT );
			$factionId = $dal->getValue( $statement );
			if( isset( $factionId ) ) {
				$rulerId = $location->ruler_id;
				if( $factionId == $dal->getValue( $statement ) ) {
					$validLocation = true;
				}
			}
		}
	}
	return ( $validLocation ? $location : null );
} 